package snapex.core.security;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.client.JdbcClientDetailsService;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Configuration
public class ClientDetailConfiguration {
	
//	@Autowired	
//	private PasswordEncoder passwordEncoder;
//	
//    @Bean   
//    public PasswordEncoder passwordEncoder() {
//        return PasswordEncoderFactories.createDelegatingPasswordEncoder();
//    }
    
//    @Autowired
//    private DataSource dataSource;
//    
//    @Bean	
//    public ClientDetailsService jdbcClientDetailsService() {  
//    	log.debug("created jdbcClientDetailsService*********");
//    	
//    	JdbcClientDetailsService clientDetailsService = new JdbcClientDetailsService(this.dataSource);
//    	
//    	//clientDetailsService.setPasswordEncoder(new BCryptPasswordEncoder());
//    	
//    	return clientDetailsService;
//    }
//    
//	@Bean	
////	@Conditional(MemoryClientStoreCondition.class)
//	public ClientDetailsService clientDetailsService() {
//		
//		Map<String,ClientDetails> clientDetailsStore = new HashMap();
//		
//		
//		BaseClientDetails zuulclient = new BaseClientDetails("snapex-gateway","","openid","authorization_code,refresh_token","CLIENT");
//		zuulclient.setClientSecret(passwordEncoder.encode("snapex-gateway-secret"));		
//		Set<String> registeredRedirectUris = new HashSet<String>();		
//		registeredRedirectUris.add("http://localhost:9901/authenticate");
//		registeredRedirectUris.add("http://127.0.0.1:9901/authenticate");
//		
//		registeredRedirectUris.add("https://localhost:9943/authenticate");
//		registeredRedirectUris.add("https://127.0.0.1:9943/authenticate");
//		zuulclient.setRegisteredRedirectUri(registeredRedirectUris);
//		zuulclient.setAutoApproveScopes(Arrays.asList("openid"));
//		clientDetailsStore.put("snapex-gateway", zuulclient);
//		
//		log.debug("***Added client:{}",zuulclient.toString());
//		
//		BaseClientDetails snapexService = new BaseClientDetails("snapex-service","","openid","authorization_code,refresh_token","CLIENT");
//		snapexService.setClientSecret(passwordEncoder.encode("snapex-service-secret"));
//		clientDetailsStore.put("snapex-service", snapexService);
//		log.debug("***Added client:{}",snapexService.toString());
//		
//		
////		BaseClientDetails snapexRegistration = new BaseClientDetails("snapex-registration","","openid","authorization_code,refresh_token","CLIENT");
////		snapexRegistration.setClientSecret(passwordEncoder.encode("snapex-registration-secret"));
////		clientDetailsStore.put("snapex-registration", snapexRegistration);
////		
////		Set<String> snapexRegistrationRedirectUris = new HashSet<String>();		
////		snapexRegistrationRedirectUris.add("http://localhost:9701/authenticate");
////		snapexRegistration.setRegisteredRedirectUri(snapexRegistrationRedirectUris);
////		snapexRegistration.setAutoApproveScopes(Arrays.asList("openid"));
////		
////		log.debug("***Added client:{}",snapexRegistration.toString());
//		
//		
//		InMemoryClientDetailsService clientDetailsService = new InMemoryClientDetailsService();
//		clientDetailsService.setClientDetailsStore(clientDetailsStore);
//		return clientDetailsService;
//	}
//		
//	static class MemoryClientStoreCondition extends SpringBootCondition{
//
//		@Override
//		public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
//	
//			ConditionMessage.Builder message = ConditionMessage.forCondition("snapex.security.clientStore Condition");
//			
//			String tokenStore = context.getEnvironment().getProperty("snapex.security.clientStore");
//			if(tokenStore == null || "MEMORY".equals(tokenStore)) {
//				return ConditionOutcome.match(message.foundExactly("Memory clientStore"));
//			}
//			return ConditionOutcome.noMatch(message.didNotFind("Memory clientStore").atAll());
//		}		
//	}
}
